package com.asiainfo.fc.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Properties;

public class FileHelper {

	/**
	 * convert file size
	 * 
	 * @param fileSize
	 * @return
	 */
	public static String toFileSize(Long fileSize) {
		String str = "";
		NumberFormat format = new DecimalFormat("##.00");
		if (fileSize.longValue() >= 1073741824L)
			str = format.format((fileSize.longValue() + 0.01D) / 1073741824.0D)
					+ " GB";
		else if (fileSize.longValue() >= 1048576L)
			str = format.format((fileSize.longValue() + 0.01D) / 1048576.0D)
					+ " MB";
		else if (fileSize.longValue() > 1024L)
			str = format.format((fileSize.longValue() + 0.01D) / 1024.0D)
					+ " KB";
		else {
			str = fileSize + " Byte";
		}

		return str;
	}

	/**
	 * serializeObject
	 * 
	 * @param filePath
	 * @param obj
	 */

	public static void serializeObject(String filePath, Object obj) {
		try {
			FileOutputStream fos = new FileOutputStream(filePath);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(obj);
			oos.flush();
			oos.close();
		} catch (Exception e) {

			e.printStackTrace();
		}

	}

	public static Object unSerializeObject(String filePath) {
		try {
			FileInputStream fis = new FileInputStream(filePath);
			ObjectInputStream ois = new ObjectInputStream(fis);
			Object obj = ois.readObject();
			return obj;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * @param basepath
	 * @return
	 */
	public static Properties loadProperties(String basepath) {

		InputStream fStream;
		Properties prop = new Properties();

		try {
			File file = new File(basepath);
			if (!file.exists()) {
				Properties properties = new Properties();
				newProperties(basepath, properties);
			}

			fStream = new FileInputStream(basepath);
			prop.load(fStream);
			fStream.close();
		} catch (Exception e) {

			e.printStackTrace();
		}
		return prop;

	}

	/**
	 * @param basePath
	 * @param prop
	 */

	public static void saveProperties(String basePath, Properties prop) {

		try {
			OutputStream fos = new FileOutputStream(basePath);

			prop.store(fos, "application");

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	public static void newProperties(String basePath, Properties prop) {

		try {
			OutputStream fos = new FileOutputStream(basePath);
			prop.setProperty("jdk_class_path", "");
			prop.setProperty("customer_class_path", "");
			prop.setProperty("jdk_path", "");
			prop.setProperty("output_path", "");
			prop.setProperty("java_version", "");

			prop.setProperty("show_line", "true");
			prop.setProperty("highter_line", "true");
			// prop.setProperty("highter_caret", "true");
			prop.store(fos, "application");

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	/**
	 * read full text
	 * 
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static String readFullText(String path) throws IOException {
		StringBuffer sbBuffer = new StringBuffer();
		FileReader reader = new FileReader(path);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line;
		while ((line = bufferedReader.readLine()) != null) {
			sbBuffer.append(line + "\n");
		}
		bufferedReader.close();
		reader.close();
		return sbBuffer.toString();

	}

	/**
	 * @return
	 */
	public static String getCurrentDir() {
		try {
			return URLDecoder.decode(FileHelper.class.getProtectionDomain()
					.getCodeSource().getLocation().getFile(), "UTF-8");
		} catch (UnsupportedEncodingException e) {

			e.printStackTrace();
		}
		return "";
	}

	/**
	 * @param sourcePath
	 * @param targetFile
	 * @throws Exception
	 */
	public static void saveFile(String sourcePath, String targetFile)
			throws Exception {

		FileInputStream fs = new FileInputStream(sourcePath);
		FileOutputStream fout = new FileOutputStream(targetFile);
		byte[] bytes = new byte[1024];
		int c = -1;
		while ((c = fs.read(bytes)) > 0) {
			fout.write(bytes, 0, c);
		}
		fs.close();
		fout.close();

	}

	/**
	 * @param fileName
	 * @return
	 */
	public static String getFileName(String fileName) {
		int index = fileName.lastIndexOf("\\");
		if (index == -1) {
			index = fileName.lastIndexOf("/");
		}
		return fileName.substring(index + 1);
	}

	/**
	 * @param fileName
	 * @return
	 */
	public static String getFileNameWithoutExtName(String fileName) {
		int index = fileName.lastIndexOf("\\");
		if (index == -1) {
			index = fileName.lastIndexOf("/");
		}
		int dot = fileName.lastIndexOf(".");
		return fileName.substring(index + 1, dot);
	}

	/**
	 * @param filePath
	 * @return
	 */
	public static boolean isExists(String filePath)

	{
		File file = new File(filePath);
		return file.exists();

	}

	/**
	 * @param filePath
	 * @param content
	 * @return
	 */
	public static boolean write2File(String filePath, String content) {
		try {
			FileOutputStream fout = new FileOutputStream(filePath);
			byte[] bytes = content.getBytes();
			try {
				fout.write(bytes);
			} catch (IOException e) {

				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {

			e.printStackTrace();
			return false;
		}
		return true;

	}

	private FileHelper() {
	}
}
